package com.comm.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * 读写doc
 * 
 * 
 */
public class POIUtil {
	/**
	 * 读入doc
	 * 
	 * @param doc
	 * @return
	 * @throws Exception
	 */
	public static String readDoc(String doc) throws Exception {
		// 创建输入流读取DOC文件
		FileInputStream in = new FileInputStream(new File(doc));
		WordExtractor extractor = null;
		String text = null;
		// 创建WordExtractor
		extractor = new WordExtractor(in);
		// 对DOC文件进行提取
		text = extractor.getText();
		return text;
	}

	/**
	 * 写出doc
	 * 
	 * @param path
	 * @param content
	 * @return
	 */
	public static boolean writeDoc(String path, String content) {
		boolean w = false;
		try {

			// byte b[] = content.getBytes("ISO-8859-1");
			byte b[] = content.getBytes();

			ByteArrayInputStream bais = new ByteArrayInputStream(b);

			POIFSFileSystem fs = new POIFSFileSystem();
			DirectoryEntry directory = fs.getRoot();

			DocumentEntry de = directory.createDocument("WordDocument", bais);

			FileOutputStream ostream = new FileOutputStream(path);

			fs.writeFilesystem(ostream);

			bais.close();
			ostream.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return w;
	}

	public void testReadDoc() {
		try {
			String text = POIUtil.readDoc("c://葛志宇个人简历.doc");
			System.out.println(text);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void testWriteDoc() {
		String wr;
		try {
			wr = POIUtil.readDoc("c://葛志宇个人简历.doc");

			boolean b = POIUtil.writeDoc("c:\\葛志宇个人forload.doc", wr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static boolean createFile(String url, String path) {
		if(StringUtil.isBlank(url)){return false;}
		
		String _url = url ;
		if(!url.startsWith("http")){
			_url="http://"+_url;
		}
		String wr = "";
		try {
			wr = MyHttpClient.getResponseBodyAsString(_url);
			 
			System.out.println(wr);
		} catch (HttpException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		try {
			//FileUtils.writeByteArrayToFile(new File(path), wr);
			FileUtils.writeStringToFile(new File(path), wr);
		} catch (IOException e) {
			return false;
		}
		return true;// "c:\\forload.doc" ,HTML内容
	}

	public static void main(String[] args) {
		String url = "http://localhost:8080/hr/previewResume.do?resumeid=11";
		String wr = "";
		try {
			wr = MyHttpClient.getHtmlSourceCode(url);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			FileUtils.writeStringToFile(new File("c:\\FileUtilsload.doc"), wr);
		} catch (IOException e) {
			 
		}
		POIUtil.writeDoc("c:\\forload.doc", wr);
	}

}